<script setup>
import {ref, defineEmits, defineProps, watchEffect, reactive} from "vue";
import {ElMessage} from "element-plus";
import axios from "axios";
    //自定义属性负责接收外界传入的被修改的数据源
    const props = defineProps(["data"]);

    let form = reactive({
      categoryId:"",
      categoryName:"",
      parentId:""
    });

// 添加自定义事件
const emite = defineEmits(["editeValue"]);

watchEffect(() => {
  if (props.data) {
    Object.assign(form, props.data);
  }
});

async function saveAction() {
  try {

    // 发送 POST 请求到后端
    const response = await axios.post('/api/courseCategory/update', form);

    // 显示成功消息
    ElMessage.success('课程分类更新成功');

    // 清空表单属性
    Object.keys(form).forEach(key => {
      form[key] = "";
    });
  } catch (error) {
    console.error('更新失败:', error);

    // 显示错误消息
    ElMessage.error('课程分类更新失败');
  }
  emite("editeValue", form);
}

</script>

<template>
  <el-form :model="form">
    <el-form-item label="分类名称：" label-width="100">
      <el-input v-model="form.categoryName" :placeholder="请输入课程名称"></el-input>
    </el-form-item>

<!--    <el-form-item label="父节点：" label-width="100">-->
<!--      <el-input v-model="form.parentId" :placeholder="请选择父节点"></el-input>-->
<!--    </el-form-item>-->
  </el-form>
  <div style="display: flex; justify-content: center;">
    <el-button @click="saveAction">保存</el-button>
  </div>
</template>

<style scoped lang="less">

</style>